<template>
  <div>
    <el-table :data="tempData" style="width: 100%">
      <el-table-column type="expand">
        <template slot-scope="props">
          <el-form label-position="left" inline class="demo-table-expand">
            <el-form-item label="商品ID">
              <span>{{ props.row.goods_id }}</span>
            </el-form-item>
            <el-form-item label="商品名称">
              <span>{{ props.row.short_name }}</span>
            </el-form-item>
            <el-form-item label="商品分类">
              <span>{{ category[props.row.category - 1] }}</span>
            </el-form-item>
            <el-form-item label="商品价格">
              <span>{{ props.row.price / 100 }}</span>
            </el-form-item>
            <el-form-item label="商品库存">
              <span>{{ props.row.counts }}</span>
            </el-form-item>
            <el-form-item label="商品描述">
              <span>{{ props.row.goods_name }}</span>
            </el-form-item>
            <el-form-item label="商品图片">
              <img :src="props.row.thumb_url" alt="" style="width: 70px" />
            </el-form-item>
          </el-form>
        </template>
      </el-table-column>
      <el-table-column label="商品 ID" prop="goods_id"> </el-table-column>
      <el-table-column label="商品名称" prop="short_name"> </el-table-column>
      <el-table-column label="描述" prop="goods_name"> </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button size="mini" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
          <el-button size="mini" type="danger" @click="handleDelete(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-pagination background layout="prev, pager, next" :page-count="pagenum" @current-change="handleCurrentChange">
    </el-pagination>

  </div>
</template>

<script>
import { allgoods, deletegoods } from '@/api/index.js'
export default {
  data() {
    return {
      category: ['图书、影片', '家居生活', '服饰、箱包', '智能产品', '美食宝典'],
      //全部商品
      tableData: [],
      pageSize: 5,
      currenindex: 1,
      tempData: []
    }
  },
  mounted() {
    this.getallgoods()
  },
  methods: {
    //获取商品数据
    async getallgoods() {
      let result = await allgoods()
      if (result.data.code == 200) {
        this.tableData = result.data.msg
        this.tableData.forEach((data, index) => {
          if (index >= (this.currenindex - 1) * this.pageSize && index < this.currenindex * this.pageSize) {
            this.tempData.push(data)
          }
        })
      }
    },
    //分页点击
    handleCurrentChange(val) {
      this.currenindex = val
      this.tempData = []
      this.tableData.forEach((data, index) => {
        //index从0开始，一直比当前页面大，根据后面的代码当index大于当前页面大小的值时，就不再push进tempData
        if (index >= (this.currenindex - 1) * this.pageSize && index < this.currenindex * this.pageSize) {
          this.tempData.push(data)
        }
      })
    },
    //编辑功能
    handleEdit(index, row) {
      this.$router.push({ path: '/adminmain/update', query: { row: row } })
    },

    //删除
    handleDelete(index, row) {
      this.$confirm('是否删除？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          let result = await deletegoods({ id: row.goods_id })

          if (result.data.code == 200) {
            this.$message({
              type: 'success',
              message: '删除成功!'
            })
          }
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    }
  },
  computed: {
    pagenum() {
      return Math.ceil(this.tableData.length / this.pageSize)
    }
  }
}
</script>

<style scoped lang="less">
.el-form-item {
  margin-right: 0;
  margin-bottom: 0;
  width: 50%;
  padding-left: 20px;
}
.el-pagination {
  text-align: center;
}
</style>
